import { useEffect } from 'react';
import { useStudentStore } from './studentStore';
import { studentSelectors } from './studentSelectors';
import type { IStudent, IStudentCreateRequest, IStudentUpdateRequest } from '@/types/student/student';

/**
 * 学生管理 Hook
 * @description 提供学生管理的完整功能，包括数据操作、UI状态管理等
 */
export const useStudentManagement = () => {
  const store = useStudentStore();

  // 初始化数据
  useEffect(() => {
    if (store.students.length === 0 && !store.async.loading) {
      store.fetchStudents();
    }
  }, [store.students.length, store.async.loading]);

  return {
    // 数据状态
    students: studentSelectors.getFilteredStudents(store),
    allStudents: studentSelectors.getAllStudents(store),
    selectedStudent: store.selectedStudent,
    selectedStudents: studentSelectors.getSelectedStudents(store),
    
    // 异步状态
    isLoading: studentSelectors.isLoading(store),
    error: studentSelectors.getError(store),
    lastUpdated: studentSelectors.getLastUpdated(store),
    
    // UI状态
    showForm: store.showStudentForm,
    formMode: store.formMode,
    showDetail: store.showStudentDetail,
    showBatchPanel: store.showBatchPanel,
    
    // 选择状态
    selectedCount: studentSelectors.getSelectedCount(store),
    hasSelected: studentSelectors.hasSelectedStudents(store),
    isAllSelected: studentSelectors.isAllSelected(store),
    isIndeterminate: studentSelectors.isIndeterminate(store),
    
    // 筛选状态
    filters: store.filters,
    
    // 统计信息
    statistics: studentSelectors.getStudentStatistics(store),
    grades: studentSelectors.getAllGrades(store),
    classes: studentSelectors.getAllClasses(store),
    
    // 数据操作
    fetchStudents: store.fetchStudents,
    addStudent: store.addStudent,
    updateStudent: store.updateStudent,
    deleteStudent: store.deleteStudent,
    batchDeleteStudents: store.batchDeleteStudents,
    searchStudents: store.searchStudents,
    refresh: store.refresh,
    
    // 选择操作
    selectStudent: store.selectStudent,
    toggleSelection: store.toggleStudentSelection,
    selectAll: store.selectAllStudents,
    clearSelection: store.clearSelection,
    
    // UI操作
    openCreateForm: () => {
      store.setFormMode('create');
      store.selectStudent(null);
      store.setShowStudentForm(true);
    },
    openEditForm: (student: IStudent) => {
      store.selectStudent(student);
      store.setFormMode('edit');
      store.setShowStudentForm(true);
    },
    closeForm: () => {
      store.setShowStudentForm(false);
      store.selectStudent(null);
    },
    openDetail: (student: IStudent) => {
      store.selectStudent(student);
      store.setShowStudentDetail(true);
    },
    closeDetail: () => {
      store.setShowStudentDetail(false);
    },
    openBatchPanel: () => {
      store.setShowBatchPanel(true);
    },
    closeBatchPanel: () => {
      store.setShowBatchPanel(false);
      store.clearSelection();
    },
    
    // 筛选操作
    updateFilters: store.updateFilters,
    setSearchKeyword: store.setSearchKeyword,
    setSorting: store.setSorting,
    resetFilters: store.resetFilters,
    
    // 工具函数
    getStudentById: (id: string) => studentSelectors.getStudentById(store, id),
    getStudentByStudentId: (studentId: string) => studentSelectors.getStudentByStudentId(store, studentId),
    isStudentSelected: (studentId: string) => studentSelectors.isStudentSelected(store, studentId),
    
    // 重置操作
    reset: store.reset
  };
};

/**
 * 学生选择 Hook
 * @description 专门用于学生选择功能的轻量级Hook
 */
export const useStudentSelection = () => {
  const store = useStudentStore();

  return {
    // 选择状态
    selectedStudentIds: store.selectedStudentIds,
    selectedStudents: studentSelectors.getSelectedStudents(store),
    selectedCount: studentSelectors.getSelectedCount(store),
    hasSelected: studentSelectors.hasSelectedStudents(store),
    isAllSelected: studentSelectors.isAllSelected(store),
    isIndeterminate: studentSelectors.isIndeterminate(store),
    
    // 选择操作
    toggleSelection: store.toggleStudentSelection,
    selectAll: store.selectAllStudents,
    clearSelection: store.clearSelection,
    isStudentSelected: (studentId: string) => studentSelectors.isStudentSelected(store, studentId)
  };
};

/**
 * 学生表单 Hook
 * @description 专门用于学生表单操作的Hook
 */
export const useStudentForm = () => {
  const store = useStudentStore();

  return {
    // 表单状态
    showForm: store.showStudentForm,
    formMode: store.formMode,
    selectedStudent: store.selectedStudent,
    isLoading: studentSelectors.isLoading(store),
    error: studentSelectors.getError(store),
    
    // 表单操作
    openCreateForm: () => {
      store.setFormMode('create');
      store.selectStudent(null);
      store.setShowStudentForm(true);
    },
    openEditForm: (student: IStudent) => {
      store.selectStudent(student);
      store.setFormMode('edit');
      store.setShowStudentForm(true);
    },
    closeForm: () => {
      store.setShowStudentForm(false);
      store.selectStudent(null);
    },
    
    // 数据操作
    addStudent: async (studentData: IStudentCreateRequest) => {
      await store.addStudent(studentData);
    },
    updateStudent: async (id: string, studentData: Omit<IStudentUpdateRequest, 'id'>) => {
      await store.updateStudent(id, studentData);
    },
    
    // 工具函数
    getStudentByStudentId: (studentId: string) => studentSelectors.getStudentByStudentId(store, studentId)
  };
};

/**
 * 学生筛选 Hook
 * @description 专门用于学生筛选功能的Hook
 */
export const useStudentFilter = () => {
  const store = useStudentStore();

  return {
    // 筛选状态
    filters: store.filters,
    filteredStudents: studentSelectors.getFilteredStudents(store),
    
    // 筛选选项
    grades: studentSelectors.getAllGrades(store),
    classes: studentSelectors.getAllClasses(store),
    
    // 筛选操作
    updateFilters: store.updateFilters,
    setSearchKeyword: store.setSearchKeyword,
    setSorting: store.setSorting,
    resetFilters: store.resetFilters,
    
    // 搜索操作
    searchStudents: store.searchStudents
  };
};

/**
 * 学生统计 Hook
 * @description 专门用于学生统计信息的Hook
 */
export const useStudentStatistics = () => {
  const store = useStudentStore();

  return {
    // 统计数据
    statistics: studentSelectors.getStudentStatistics(store),
    activeStudents: studentSelectors.getActiveStudents(store),
    inactiveStudents: studentSelectors.getInactiveStudents(store),
    graduatedStudents: studentSelectors.getGraduatedStudents(store),
    
    // 分组数据
    grades: studentSelectors.getAllGrades(store),
    classes: studentSelectors.getAllClasses(store),
    
    // 工具函数
    getStudentsByGrade: (grade: string) => studentSelectors.getStudentsByGrade(store, grade),
    getStudentsByClass: (className: string) => studentSelectors.getStudentsByClass(store, className),
    getStudentsByGender: (gender: 'male' | 'female' | 'other') => studentSelectors.getStudentsByGender(store, gender)
  };
};